<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.applicationName" placeholder="名称"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="getApps">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="getApps">导入</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="handleDesign">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <!--列表-->
        <el-table :data="apps" highlight-current-row v-loading="listLoading" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column v-if="false" prop="id" label="id"/>
            <el-table-column prop="name" label="名称" width="150">
            </el-table-column>
            <el-table-column prop="procId" label="key" width="100">
            </el-table-column>
            <el-table-column prop="deploymentId" label="部署id" width="200">
            </el-table-column>
            <el-table-column prop="desc" label="描述" width="100">
            </el-table-column>
            <el-table-column label="操作" width="550">
                <template scope="scope">
                    <el-button size="small" type="warning" @click="previewSvg(scope.row)">查看</el-button>
                    <!--<el-button size="small" type="info" @click="previewSvg(scope.row)">查看</el-button>-->
                    <el-button size="small" type="success" @click="bpmnEdit(scope.row)">编辑</el-button>
                    <el-button size="small" type="danger" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                   <!-- <el-button v-if="scope.row.releaseStatus==2" size="small" type="warning"
                               @click="handleUpDown(scope.$index, scope.row)">上线
                    </el-button>
                    <el-button v-else size="small" type="warning" @click="handleUpDown(scope.$index, scope.row)">下线
                    </el-button>-->
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-pagination layout="total, sizes, prev, pager, next" @current-change="handleCurrentChange"
                           @size-change="handleSizeChange" :page-size="page.pageSize"
                           :total="page.total" style="float:right;">
            </el-pagination>
        </el-col>


        <!--编辑界面-->
        <el-dialog title="编辑" :visible.sync="editFormVisible">
            <el-form :model="editForm" label-width="120px" ref="editForm">
                <el-form-item label="应用名称" prop="applicationName">
                    <el-input v-model="editForm.applicationName" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="应用编码" prop="passId">
                    <el-input v-model="editForm.passId" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="PassToken" prop="passToken">
                    <el-input v-model="editForm.passToken" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="应用访问地址" prop="appUrl">
                    <el-input v-model="editForm.appUrl" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="应用描述" prop="description">
                    <el-input type="textarea" v-model="editForm.description"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="editFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
            </div>
        </el-dialog>

        <!--新增界面-->
        <el-dialog title="新增" :visible.sync="addFormVisible">
            <el-form :model="addForm" label-width="120px" ref="addForm">
                <el-form-item label="应用名称" prop="applicationName">
                    <el-input v-model="addForm.applicationName" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="应用编码" prop="passId">
                    <el-input v-model="addForm.passId" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="PassToken" prop="passToken">
                    <el-input v-model="addForm.passToken" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="应用访问地址" prop="appUrl">
                    <el-input v-model="addForm.appUrl" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="应用描述" prop="description">
                    <el-input type="textarea" v-model="addForm.description"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>

        <!-- 流程设计器 -->
        <el-dialog v-dialogDrag title="流程图" :visible.sync="bpmnDialogVisible" width="1100px">
            <slot name="-" style="border: none;padding: 0px;margin: 0px;">
                <VueBpmn style="overflow: hidden;height: 700px;" product="flowable"
                         @processSave="processSave" ref="childRef"></VueBpmn>
            </slot>
        </el-dialog>

    </section>
</template>

<script>
    import Bus from '@/common/evenBus'
    import NProgress from 'nprogress'
    import VueBpmn from "@/components/bpmn/VueBpmn";
    import {addModel, getAppList,deleteProcessDefination} from "@/api/api";

    export default {
        name: "TemplateMng",
        components: {VueBpmn},
        inject:['reload'],
        data() {
            return {
                // modeler: {
                //     type: Object
                // },
                // 设计器属性开始
                bpmnDialogVisible: false,
                columns: [{
                    key: 'procId',
                    title: '流程定义ID'
                }, {
                    key: 'name',
                    title: '流程名称'
                }, {
                    key: 'status',
                    title: '状态',
                    formatter: (row, key) => {
                        if (row[key] == 0) {
                            return "未发布";
                        }
                        return "已发布"
                    }
                }, {
                    key: 'ora',
                    title: '操作',
                    operator: [{
                        name: "发布",
                        type: "text",
                        click: (row) => {
                            this.put(this.Apis.processDeploy.replace("${id}", row.id), {}, res => {
                                console.log(res)
                            });
                        },
                        hidden: (row) => {
                            return row.status == 1;
                        }
                    }, {
                        name: "删除",
                        type: "text",
                        click: (row) => {
                            this.delete(this.Apis.processRemove.replace("${id}", row.id), {}, res => {
                                console.log(res)
                                if (res.code == 200) {
                                    this.$refs.elementTable.reload();
                                }
                            })
                        }
                    }]
                }],
                // 设计器属性结束
                filters: {
                    applicationName: '',
                    passId: ''
                },
                apps: [],

                page: {
                    pageNo: 1,
                    pageSize: 10,
                    total: 400
                },
                listLoading: false,
                sels: [],//列表选中列
                editFormVisible: false,//编辑界面是否显示
                editLoading: false,
                editFormRules: {
                    applicationName: [
                        {required: true, message: '请输入应用名称', trigger: 'blur'}
                    ],
                    passId: [
                        {required: true, message: '请输入passId', trigger: 'blur'}
                    ],
                    passToken: [
                        {required: true, message: '请输入passToken', trigger: 'blur'}
                    ],
                    appUrl: [
                        {required: true, message: '请输入访问地址', trigger: 'blur'}
                    ],
                    description: [
                        {required: true, message: '请输入应用描述', trigger: 'blur'}
                    ],
                },
                //编辑界面数据
                editForm: {
                    procId: '',
                    name: '',
                    desc: '',
                    creator: '',
                    status: ''
                },

                addFormVisible: false,//新增界面是否显示
                addLoading: false,
                addFormRules: {
                    applicationName: [
                        {required: true, message: '请输入应用名称', trigger: 'blur'}
                    ],
                    passId: [
                        {required: true, message: '请输入passId', trigger: 'blur'}
                    ],
                    passToken: [
                        {required: true, message: '请输入passToken', trigger: 'blur'}
                    ],
                    appUrl: [
                        {required: true, message: '请输入访问地址', trigger: 'blur'}
                    ],
                    description: [
                        {required: true, message: '请输入应用描述', trigger: 'blur'}
                    ],
                },
                //新增界面数据
                addForm: {
                    procId: '',
                    name: '',
                    desc: '',
                    creator: '',
                    status: ''
                }

            }
        },
        methods: {
            bpmnEdit(row) {
                let data= row.xmlStr
                this.bpmnDialogVisible = true;
                Bus.$on("bpmnModeler", (message) => {
                    message.importXML(data, (err) => {
                        // if (err) {
                        //     this.$message.info('导入失败');
                        // } else {
                        //     this.$message.success('导入成功');
                        // }
                    });
                })

                // bpmnModeler.importXML(data, (err) => {
                //     if (err) {
                //         this.$message.info('导入失败');
                //     } else {
                //         this.$message.success('导入成功');
                //     }
                // });
                // this.modeler.importXML(row.xmlStr, (err) => {
                //     if (err) {
                //         this.$message.info('导入失败');
                //     } else {
                //         this.$message.success('导入成功');
                //     }
                // });
                // console.log("279===")
                // console.log(this.$refs.childRef);
                // this.$refs.childRef.bpmnModeler.importXML(row.xmlStr, (err) => {
                //     if (err) {
                //         this.$message.info('导入失败');
                //     } else {
                //         this.$message.success('导入成功');
                //         this.bpmnDialogVisible = true;
                //     }
                // });
            },
            previewSvg(row) {
                // let myWindow=window.open('#',"_blank");
                // myWindow.document.write('<div>新页面</div>')
                window.open("http://127.0.0.1:9999/api/model/preview?a=" + row.id, "_blank");
            },
            //设计器方法开始
            processSave(data) {
                // alert("哈哈哈哈");
                console.log(data);
                addModel(data).then(res => {
                    this.$message({
                        type:"success",
                        message:"success"
                    })
                    //自己的业务逻辑代码：服务端返回的参数都封装在了data里面
                    //alert(res.errmsg);
                }).catch(e => {
                    console.log("请求错误");
                    console.log(e);
                    //自己的业务逻辑代码：服务端返回的异常都封装在了e里面
                });
            },
            deploy() {
               // alert("哈哈哈哈");
            },
            //设计器方法结束
            handleDesign() {
                this.bpmnDialogVisible = true;
                // this.$router.go(0); 传参
            },

            closeAddDialog() {
                this.$refs['addForm'].resetFields();
            },
            statusFormat(row, column) {
                if (row.releaseStatus === 1) {
                    return '已上线'
                } else {
                    return '未上线'
                }
            },
            typeFormat(row, column) {
                if (row.applicationType === 1) {
                    return '业务应用'
                } else if (row.applicationType === 2) {
                    return '系统应用'
                } else {
                    return '第三方应用'
                }
            },
            handleCurrentChange(val) {
                this.page.pageNo = val;
                this.getApps();
            },
            handleSizeChange(val) {
                this.page.pageSize = val;
                this.getApps();
            },
            //获取应用列表
            getApps() {
                let para = {
                    pageNo: this.page.pageNo,
                    pageSize: this.page.pageSize,
                    applicationName: this.filters.applicationName,
                    passId: this.filters.passId
                };
                this.listLoading = true;
                NProgress.start();
                getAppList().then((res) => {
                    this.apps = res.data;
                    this.listLoading = false;
                    NProgress.done();
                }).catch(error => {
                    console.log(error);
                });
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    deleteProcessDefination(row.deploymentId).then((res) => {
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.reload()
                       console.log(res.errmsg)
                    }).catch(error => {
                        console.log(error);
                    });

                    // let paraBinding = [{"appId": row.applicationId}];
                    // this.$message({
                    //     message: '该应用被配置中心使用，无法删除',
                    //     type: 'error'
                    // });
                    // this.$message({
                    //     message: '删除成功',
                    //     type: 'success'
                    // });
                });
            },
        },

        handleConfig(index, row) {
            sessionStorage.setItem("currentAppId", row.applicationId);
            this.$router.push('/uniConf/:id');
            this.$router.go(0);
        },
        selsChange: function (sels) {
            this.sels = sels;
        },
        //批量删除
        batchRemove: function () {
            var ids = [];
            this.sels.forEach(function (v, i) {
                ids.push({"appId": v.appId});
            });
            this.$confirm('确认删除选中记录吗？', '提示', {
                type: 'warning'
            }).then(() => {
                this.listLoading = true;
                //NProgress.start();
                // batchRemoveApp(ids).then((res) => {
                //     this.listLoading = false;
                //     //NProgress.done();
                //     this.$message({
                //         message: '删除成功',
                //         type: 'success'
                //     });
                //     this.getApps();
                // });
            }).catch(() => {

            });
        },

        mounted() {
            this.getApps();
        }
    }

</script>

<style scoped>

</style>
